/**
 * Copyright (C) 2015 kmy-framework-base Project
 *               Author: Chris
 *               Date: 2015年11月7日
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zmq.ha.client.core;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.zmq.ha.client.util.SnowflakeUtil;

/**
 * Name: Publisher.java
 * ProjectName: [kmy-framework-base]
 * Package: [com.kmy.framework.rabbitmq.recover.core.Publisher.java]
 * Description: TODO  
 * 
 * @since JDK1.7
 * @see
 *
 * @author: Chris
 * @date: 2015年11月7日 下午5:13:27
 *
 * Update-User: @author
 * Update-Time:
 * Update-Remark:
 * 
 * Check-User:
 * Check-Time:
 * Check-Remark:
 * 
 * Company: kmhc
 * Copyright: kmhc
 */

public class ExecutePublisher{
	
	final Channel channel;
	final String exchange;
	final String routingkey;
	AMQP.BasicProperties.Builder builder = null;
	
	/** 
	 * @Title: Publisher
	 * @Description: TODO
	 * @param @param channel
	 * @param @param exchange
	 * @param @param routingkey         
	 * @throws 
	 */ 
	public ExecutePublisher(Channel channel, String exchange, String routingkey) {
		super();
		this.channel = channel;
		this.exchange = exchange;
		this.routingkey = routingkey;
		
		builder = new AMQP.BasicProperties().builder();
		builder.deliveryMode(2);
		builder.priority(0);
		builder.contentType("text/plain");
	}

	/**
	 * 
	 * @Title: publishMessage
	 * @Description: 发送消息方法,强制设置为事务类型,在事务失败的时候会抛出错误
	 * @param @param body
	 * @param @throws IOException
	 * @param @throws InterruptedException     
	 * @return void     
	 * @throws
	 */
	public void publishMessage( byte[] body ) throws IOException, InterruptedException{
		this.channel.confirmSelect();
		builder.messageId(String.valueOf(SnowflakeUtil.get().nextId()));
		this.channel.basicPublish(this.exchange, this.routingkey, builder.build(), body);
		this.channel.waitForConfirmsOrDie();
	}

	/**
	 * 
	 * @Title: withBasicProperties
	 * @Description: 手动指定BasicProperties.Builder
	 * @param @param builder
	 * @param @return     
	 * @return ExecutePublisher     
	 * @throws
	 */
	public ExecutePublisher withBasicProperties( BasicProperties.Builder builder  ){
		this.builder = builder;
		return this;
	}
	
	
	public void closeChannel(){
		try {
			this.channel.close();
		} catch (IOException | TimeoutException e) {
			e.printStackTrace();
		}
	}

	public String getExchange() {
		return exchange;
	}

	public String getRoutingkey() {
		return routingkey;
	}
}